﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Threading;

namespace ProAG_Tetris_WPF
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

            // Init new GameContext Instance
            Game gameContext = new Game();
            gameContext.Width = 450;
            gameContext.Height = 600;

            // Set Window Options
            Title = "ProAG Tetris";
            Width = gameContext.Width + 15;
            Height = gameContext.Height + 15;

            // Create 2D GameCanvas and set it's width and height
            gameContext.GameCanvas = new Canvas();
            gameContext.GameCanvas.Width = gameContext.Width;
            gameContext.GameCanvas.Height = gameContext.Height;
            gameContext.GameCanvas.Background = Brushes.DarkOrange;

            // Create PlayerCharacter 
            PlayerCharacter playerCharacter = new PlayerCharacter(gameContext, Brushes.DarkCyan, 50, 50);


            // Place PlayerCharacter into GameCanvas
            gameContext.GameCanvas.Children.Add(playerCharacter.PlayerRect);

            // Create Viewbox to autoscale content
            Viewbox gameViewbox = new Viewbox();
            gameViewbox.Width = gameContext.Width;
            gameViewbox.Height = gameContext.Height;
            gameViewbox.Child = (UIElement)gameContext.GameCanvas;
            gameViewbox.Stretch = Stretch.Fill;

            // Assign Viewbox to Window Content
            Content = gameViewbox;

            // Manipulate PlayerCharacter's position
            playerCharacter.Move(PlayerCharacter.Scope.Absolute, 10, 10);

            // Create Pseudo GameLoop-Timer
            DispatcherTimer gameTimer = new DispatcherTimer();
            gameTimer.Interval = TimeSpan.FromMilliseconds(10);
            gameTimer.Tick += (s, a) =>
                {
                    if (Keyboard.IsKeyDown(Key.Left))
                    {
                        playerCharacter.XPos -= 5;
                    }
                    else if (Keyboard.IsKeyDown(Key.Right))
                    {
                        playerCharacter.XPos += 5;
                    }
                };

            gameTimer.Start();
        }
    }
}
